<?php
namespace App\Controller;

use Think\Cache;

class IndexController extends ApiController
{
    //首页轮播图显示
       public function lunbo(){
           if(IS_POST){
               $city_id=I('post.city_id');
               if(empty($city_id)){
                   $city_id=2;
               }
               $data=D('ad')->field('pic_first,pic_two,pic_three,pic_four,pic_five,pic_six')->where("city_id={$city_id}")->select();
               $image = new \Think\Image();
              if(!empty($data)){
                  foreach ($data as $k=>&$v){
                      if(is_file('.'.$v['pic_first'])){
                          $original=$v['pic_first'];
                          $a=explode('/',$v['pic_first']);
                          $a[4]='thumbapp_'.$a[4];
                          $v['pic_first']=implode('/',$a);

                          if(!is_file('.'.$v['pic_first'])){
                              //生成缩略图
                              $image->open('.'.$original);
                              $image->thumb(500,500)->save('.'.$v['pic_first']);
                              $v['pic_first']=C('api_address').$v['pic_first'];
                          }else{
                              $v['pic_first']=C('api_address').$v['pic_first'];
                          }
                      }
                      if(is_file('.'.$v['pic_two'])){
                          $original=$v['pic_two'];
                          $a=explode('/',$v['pic_two']);
                          $a[4]='thumbapp_'.$a[4];
                          $v['pic_two']=implode('/',$a);

                          if(!is_file('.'.$v['pic_two'])){
                              //生成缩略图
                              $image->open('.'.$original);
                              $image->thumb(500,500)->save('.'.$v['pic_two']);
                              $v['pic_two']=C('api_address').$v['pic_two'];
                          }else{
                              $v['pic_two']=C('api_address').$v['pic_two'];
                          }
                      }
                      if(is_file('.'.$v['pic_three'])){
                          $original=$v['pic_three'];
                          $a=explode('/',$v['pic_three']);
                          $a[4]='thumbapp_'.$a[4];
                          $v['pic_three']=implode('/',$a);

                          if(!is_file('.'.$v['pic_three'])){
                              //生成缩略图
                              $image->open('.'.$original);
                              $image->thumb(500,500)->save('.'.$v['pic_three']);
                              $v['pic_three']=C('api_address').$v['pic_three'];
                          }else{
                              $v['pic_three']=C('api_address').$v['pic_three'];
                          }
                      }
                      if(is_file('.'.$v['pic_four'])){
                          $original=$v['pic_four'];
                          $a=explode('/',$v['pic_four']);
                          $a[4]='thumbapp_'.$a[4];
                          $v['pic_four']=implode('/',$a);

                          if(!is_file('.'.$v['pic_four'])){
                              //生成缩略图
                              $image->open('.'.$original);
                              $image->thumb(500,500)->save('.'.$v['pic_four']);
                              $v['pic_four']=C('api_address').$v['pic_four'];
                          }else{
                              $v['pic_four']=C('api_address').$v['pic_four'];
                          }
                      }
                      if(is_file('.'.$v['pic_five'])){
                          $original=$v['pic_five'];
                          $a=explode('/',$v['pic_five']);
                          $a[4]='thumbapp_'.$a[4];
                          $v['pic_five']=implode('/',$a);

                          if(!is_file('.'.$v['pic_five'])){
                              //生成缩略图
                              $image->open('.'.$original);
                              $image->thumb(500,500)->save('.'.$v['pic_five']);
                              $v['pic_five']=C('api_address').$v['pic_five'];
                          }else{
                              $v['pic_five']=C('api_address').$v['pic_five'];
                          }
                      }
                      if(is_file('.'.$v['pic_six'])){
                          $original=$v['pic_six'];
                          $a=explode('/',$v['pic_six']);
                          $a[4]='thumbapp_'.$a[4];
                          $v['pic_six']=implode('/',$a);

                          if(!is_file('.'.$v['pic_six'])){
                              //生成缩略图
                              $image->open('.'.$original);
                              $image->thumb(500,500)->save('.'.$v['pic_six']);
                              $v['pic_six']=C('api_address').$v['pic_six'];
                          }else{
                              $v['pic_six']=C('api_address').$v['pic_six'];
                          }
                      }
                  }
              }else{
                  $data=D('ad')->field('pic_first,pic_two,pic_three,pic_four,pic_five,pic_six')->where("city_id=2")->select();
                  foreach ($data as $k=>&$v){
                      if(is_file('.'.$v['pic_first'])){
                          $original=$v['pic_first'];
                          $a=explode('/',$v['pic_first']);
                          $a[4]='thumbapp_'.$a[4];
                          $v['pic_first']=implode('/',$a);

                          if(!is_file('.'.$v['pic_first'])){
                              //生成缩略图
                              $image->open('.'.$original);
                              $image->thumb(500,500)->save('.'.$v['pic_first']);
                              $v['pic_first']=C('api_address').$v['pic_first'];
                          }else{
                              $v['pic_first']=C('api_address').$v['pic_first'];
                          }
                      }
                      if(is_file('.'.$v['pic_two'])){
                          $original=$v['pic_two'];
                          $a=explode('/',$v['pic_two']);
                          $a[4]='thumbapp_'.$a[4];
                          $v['pic_two']=implode('/',$a);

                          if(!is_file('.'.$v['pic_two'])){
                              //生成缩略图
                              $image->open('.'.$original);
                              $image->thumb(500,500)->save('.'.$v['pic_two']);
                              $v['pic_two']=C('api_address').$v['pic_two'];
                          }else{
                              $v['pic_two']=C('api_address').$v['pic_two'];
                          }
                      }
                      if(is_file('.'.$v['pic_three'])){
                          $original=$v['pic_three'];
                          $a=explode('/',$v['pic_three']);
                          $a[4]='thumbapp_'.$a[4];
                          $v['pic_three']=implode('/',$a);

                          if(!is_file('.'.$v['pic_three'])){
                              //生成缩略图
                              $image->open('.'.$original);
                              $image->thumb(500,500)->save('.'.$v['pic_three']);
                              $v['pic_three']=C('api_address').$v['pic_three'];
                          }else{
                              $v['pic_three']=C('api_address').$v['pic_three'];
                          }
                      }
                      if(is_file('.'.$v['pic_four'])){
                          $original=$v['pic_four'];
                          $a=explode('/',$v['pic_four']);
                          $a[4]='thumbapp_'.$a[4];
                          $v['pic_four']=implode('/',$a);

                          if(!is_file('.'.$v['pic_four'])){
                              //生成缩略图
                              $image->open('.'.$original);
                              $image->thumb(500,500)->save('.'.$v['pic_four']);
                              $v['pic_four']=C('api_address').$v['pic_four'];
                          }else{
                              $v['pic_four']=C('api_address').$v['pic_four'];
                          }
                      }
                      if(is_file('.'.$v['pic_five'])){
                          $original=$v['pic_five'];
                          $a=explode('/',$v['pic_five']);
                          $a[4]='thumbapp_'.$a[4];
                          $v['pic_five']=implode('/',$a);

                          if(!is_file('.'.$v['pic_five'])){
                              //生成缩略图
                              $image->open('.'.$original);
                              $image->thumb(500,500)->save('.'.$v['pic_five']);
                              $v['pic_five']=C('api_address').$v['pic_five'];
                          }else{
                              $v['pic_five']=C('api_address').$v['pic_five'];
                          }
                      }
                      if(is_file('.'.$v['pic_six'])){
                          $original=$v['pic_six'];
                          $a=explode('/',$v['pic_six']);
                          $a[4]='thumbapp_'.$a[4];
                          $v['pic_six']=implode('/',$a);

                          if(!is_file('.'.$v['pic_six'])){
                              //生成缩略图
                              $image->open('.'.$original);
                              $image->thumb(500,500)->save('.'.$v['pic_six']);
                              $v['pic_six']=C('api_address').$v['pic_six'];
                          }else{
                              $v['pic_six']=C('api_address').$v['pic_six'];
                          }
                      }
                  }
              }
                $appdata=[];
                    $appdata[0]['picture_mid']=$data[0]['pic_first'];
                    $appdata[1]['picture_mid']=$data[0]['pic_two'];
                    $appdata[2]['picture_mid']=$data[0]['pic_three'];
                    $appdata[3]['picture_mid']=$data[0]['pic_four'];
                    $appdata[4]['picture_mid']=$data[0]['pic_five'];
                    $appdata[5]['picture_mid']=$data[0]['pic_six'];

               if(!empty($appdata)){
                   json_Response(200,'传输成功',$appdata,1);
               }else{
                   json_Response(-200,'传输失败',$appdata,1);
               }
           }else{
               json_Response(-200,'传输失败',null,1);
           }

       }
      //首页商品展示
       public function goods(){
           if(IS_POST){
               $p=(int)I('post.p');
               $pagesize=(int)I('post.pagesize');
               if(!is_numeric($p)||!is_numeric($pagesize)){
                   json_Response(-200, '参数错误', [], 2);
               }
               if($p==''||$p<=0){
                   $p=1;
               }
               if($pagesize==''){
                   $pagesize=20;
               }
               $data=D('seller_goods')
                   ->alias('a')
                   ->field('a.id,a.goods_name,b.picture_mid,a.seller_id,a.supplier_id')
                   ->join('INNER JOIN __GOODSPIC__ b ON b.goods_id=a.goods_id')
                   ->where("b.status=0")
                   ->group('a.id')
                   ->page($p,$pagesize)
                   ->select();
               foreach ($data as $k=>&$v){
                   $v['picture_mid']=C('api_address').$v['picture_mid'];
               }
//           dump($data);die;
               if(!empty($data)){
                   json_Response(200,'传输成功',$data,1);
               }else{
                   json_Response(-200,'传输失败',$data,1);
               }

             }else{
                    json_Response(-200,'传输失败',null,1);
             }
       }
      //会员登录
      public function login(){
          if(IS_POST){
              $phone=I('post.phone');
              $password=I('post.password');
              if (empty($phone) || empty($password)) {
                  json_Response('-200','账号和密码不能为空','null',2);
              }
              $patten2="/^\d{11}$/";//11位数字组成，验证手机号码
              if(!preg_match($patten2,$phone)){
                  json_Response('-200','手机号格式不对','null',2);
              }
              $model = D('User');
              $ini['phone'] = $phone;
              $res =$model->where($ini)->find();
              if ($res && $res['password'] == md516($password)) {

                      $res['last_login_time'] = time();
                      $m = $model->save($res);
                      unset($res['password']);
                      unset($res['password_two']);
                      if($m !== false){
                          json_Response('200','登录成功',$res,3);
                      }else{
                          json_Response('-200','登录失败','null',2);
                      }
              }else{
                  json_Response('-200','账号和密码错误','null',2);
              }
          }else{
              json_Response('-200','传输失败',null,1);
          }
      }
      //会员注册
      public function register(){
          if(IS_POST){
              // 验证注册
              $data = I('post.');
              $city=$data['city_id'];

              $data['city_id']=D('city')->where(array(
                  'id'=>array('eq',$city),
              ))->getField('id');

              if(empty($data['city_id'])){
                  json_Response('-200','请选择城市','null',2);
              }
              $data['code'] = uuid();
              $model = D('User');
              //获取推荐人的手机号
              $parent_phone = $data['parent_phone'];
              if(empty($parent_phone)){
                  $data['parent_phone'] = "13601171466";
              }else{
                  $phone = D("User")->where("phone = {$parent_phone}")->find();
                  if(empty($phone)){
                      json_Response('-200','请输入正确的推荐人帐号','null',2);
                  }
              }
              //会员姓名
              if(empty($data['username'])){
                  json_Response('-200','会员的姓名不能为空','null',2);
              }
              //手机号
              if(empty($data['phone'])){
                  json_Response('-200','手机号不能为空','null',2);
              }
              $patten2="/^1[34578]\d{9}$/";//11位数字组成，验证手机号码
              if(!preg_match($patten2,$data['phone'])){
                  json_Response('-200','手机号格式不对','null',2);
              }
              //密码
              if(empty($data['password'])){
                  json_Response('-200','密码不能为空','null',2);
              }
              if($data['password'] != $data['repassword']){
                  json_Response('-200','两次输入的密码不正确','null',2);
              }
              $data['password'] = md516($data['password']);

              $arr = $model->where("phone={$data['phone']}")->find();
              if(!empty($arr)){
                  json_Response('-200','手机号已经注册，请直接登录','null',2);
              }
              $data['create_time']=time();
              $res = $model->add($data);
              if ($res) {
//                  json_Response('200','注册成功',$res,null,3);
                  json_Response('200','注册成功','null',2);
              }else{
                  json_Response('-200',"注册失败",'null',2);
              }
          }else{
              json_Response('-200','传输失败','null',2);
          }
      }
      //省市
      public function city(){
          if(IS_POST) {
              // 验证
              $data = D('city')->where("pid=1")->select();
              foreach ($data as $k => &$v) {
                  $v['city'] = D('city')->where("pid={$v['id']}")->select();
              }
              if (empty($data)) {
                  json_Response(-200, '传输失败', $data=[], 1);
              } else {
                  json_Response(200, '传输成功', $data, 1);
              }
          }else{
              json_Response('-200','传输失败','null',2);
          }
      }
      //分类
      public function category(){
          if(IS_POST) {
              if(F('catedata')){
                  $data=F('catedata');
              }else{
                  $data = D('Category')->select();
                  foreach($data as $k=>&$v){
                      $v['brand_logo']=C('api_address').$v['brand_logo'];
                  }
                  $data=get_attr($data,0);
                  F('catedata',$data);
              }
              if (empty($data)) {
                  json_Response(-200, '传输失败', $data=[], 1);
              } else {
                  json_Response(200, '传输成功', $data, 1);
              }
          }else{
              json_Response('-200','传输失败','null',2);
          }
      }
     //关键字,分类搜索
      public function search(){
          if(IS_POST){
              $flag=I('post.flag');
              if($flag==1){//文字搜索
                  $p=(int)I('post.p');
                  $pagesize=(int)I('post.pagesize');
                  if(!is_numeric($p)||!is_numeric($pagesize)){
                      json_Response(-200, '参数错误', [], 2);
                  }
                  if($p==''||$p<=0){
                      $p=1;
                  }
                  if($pagesize==''){
                      $pagesize=20;
                  }
                  $keyword=I('post.keyword');

                  if ($keyword) {
                      $where['a.goods_name'] = array('like', "%$keyword%");
                  }else{
                      $where['a.goods_name'] = array('like', "%%");
                  }
                  $data=D('seller_goods')
                      ->alias('a')
                      ->field('a.id,a.goods_name,b.picture_mid,a.goods_price,a.sall_numbe,c.enterprise,a.seller_id,a.supplier_id')
                      ->join('INNER JOIN __GOODSPIC__ b ON b.goods_id=a.goods_id')
                      ->join('INNER JOIN __SELLER_USER__ c ON c.id=a.seller_id')
                      ->where("b.status=0")
                      ->where($where)
                      ->group('a.id')
                      ->page($p,$pagesize)
                      ->select();
                  foreach ($data as $k=>&$v){
                      $v['picture_mid']=C('api_address').$v['picture_mid'];
                  }
//                dump($data);die;
                  if(!empty($data)){
                      json_Response(200,'传输成功',$data,1);
                  }else{
                      json_Response(-200,'传输失败',$data,1);
                  }

              }elseif($flag==2){//三级分类搜索
                  $p=(int)I('post.p');
                  $pagesize=(int)I('post.pagesize');
                  if(!is_numeric($p)||!is_numeric($pagesize)){
                      json_Response(-200, '参数错误', [], 2);
                  }
                  if($p==''||$p<=0){
                      $p=1;
                  }
                  if($pagesize==''){
                      $pagesize=20;
                  }
                  $cate_id=I('post.cate_id');
                      if(empty($cate_id)){
                          json_Response(-200, '参数错误', [], 2);
                      }
                  $data=D('seller_goods')
                      ->alias('a')
                      ->field('a.id,a.goods_name,b.picture_mid,a.goods_price,a.sall_numbe,c.enterprise,a.seller_id,a.supplier_id')
                      ->join('INNER JOIN __GOODSPIC__ b ON b.goods_id=a.goods_id')
                      ->join('INNER JOIN __SELLER_USER__ c ON c.id=a.seller_id')
                      ->where("b.status=0")
                      ->where(array('category_ids'=>array('eq',$cate_id)))
                      ->group('a.id')
                      ->page($p,$pagesize)
                      ->select();
                  foreach ($data as $k=>&$v){
                      $v['picture_mid']=C('api_address').$v['picture_mid'];
                  }
//                dump($data);die;
                  if(!empty($data)){
                      json_Response(200,'传输成功',$data,1);
                  }else{
                      json_Response(-200,'传输失败',$data,1);
                  }
              }elseif ($flag==3){
                  $p=(int)I('post.p');
                  $pagesize=(int)I('post.pagesize');
                  if(!is_numeric($p)||!is_numeric($pagesize)){
                      json_Response(-200, '参数错误', [], 2);
                  }
                  if($p==''||$p<=0){
                      $p=1;
                  }
                  if($pagesize==''){
                      $pagesize=20;
                  }
                  $cate_id=I('post.cate_id');
                  if(empty($cate_id)){
                      json_Response(-200, '参数错误', [], 2);
                  }
                  $model = D("Category");
                  //获取所有的三级分类数据（二维数组，需要获取里面的所有ID）
                  $data1 = $model->where("pid={$cate_id}")->select();
                  $data2 = $model->where("id={$cate_id}")->select();
                  $data = array_merge($data1, $data2);
                  $this->assign("data2", $data2[0]);
                  $str='';
                  foreach ($data as $value) {
                      $str .= $value['id'] . ',';
                  }
                  //获取字符串类型的二、三级商品分类的id，例如:"19,20"
                  $demo = trim("$str", ',');
                  $data=D('seller_goods')
                      ->alias('a')
                      ->field('a.id,a.goods_name,b.picture_mid,a.goods_price,a.sall_numbe,c.enterprise,a.seller_id,a.supplier_id')
                      ->join('INNER JOIN __GOODSPIC__ b ON b.goods_id=a.goods_id')
                      ->join('INNER JOIN __SELLER_USER__ c ON c.id=a.seller_id')
                      ->where("b.status=0")
                      ->where("category_ids in({$demo})")
                      ->group('a.id')
                      ->page($p,$pagesize)
                      ->select();
                  foreach ($data as $k=>&$v){
                      $v['picture_mid']=C('api_address').$v['picture_mid'];
                  }
//                dump($data);die;
                  if(!empty($data)){
                      json_Response(200,'传输成功',$data,1);
                  }else{
                      json_Response(-200,'传输失败',$data,1);
                  }
              }else{

                  json_Response(-200,'参数错误',null,2);
              }

          }else{
              json_Response(-200,'传输失败',null,1);
          }
      }
      //商品详情页
      public function goods_detail(){
          if(IS_POST) {
//              //商品详情页
              $id = I('post.id');
              $seller_id = I('post.seller_id');
              $supplier_id = I('post.supplier_id');//判断自营

              if (empty($seller_id) || empty($id) || $supplier_id == "") {
                  json_Response(-200, '参数错误', $supplier_id, 2);
              }
              if (!is_numeric($seller_id) || !is_numeric($supplier_id) || !is_numeric($id)) {
                  json_Response(-200, '参数错误', $supplier_id, 2);
              }
              $data = [];
              //店铺信息
              $res = D('Seller_user')->alias('a')
                  ->field("a.id,a.enterprise,b.phone,b.linkman")
                  ->join('inner join tpshop_gongying_realname as b on a.id = b.seller_id')
                  ->where("a.id = {$seller_id}")
                  ->find();

              //判断自营$supplier_id!=0
              if ($supplier_id != '0') {//非自营
                  //点击商品
                  $goods = D('Seller_goods')->where("id = {$id}")->find();
                  if(empty($goods)){
                      json_Response(200, '传输成功', [], 1);
                  }
                  $data = D('Goods')->where("id = {$goods['goods_id']}")->find();
                  $data['seller_id'] = $res['id'];
                  $data['seller_enterprise'] = $res['enterprise'];
                  $data['seller_phone'] = $res['phone'];
                  $data['seller_linkman'] = $res['linkman'];
                  $data['goods_introduce']=str_replace("/ueditor","http://www.jitailong.shop/ueditor",$data['goods_introduce']);
                  $pattern="/src=\"(.+?)\"/";
                  preg_match_all($pattern,$data['goods_introduce'],$match);
                  $data['goods_introduce']='';
                  foreach ($match[1] as $k=>$v){
                      $data['goods_introduce'][]['picture_mid']=$v;
                  }
                  if(empty($data)){
                      json_Response(200, '传输成功', [], 1);
                  }
                  //多规格
                  $attr_radio = M('Goods_attr')->alias('a1')
                      ->field("id,a1.attr_value,attr_name")
                      ->join('left join tpshop_attribute a2 on a1.attr_id = a2.attr_id')
                      ->where("a1.goods_id = {$data['id']} and a1.zy_goods_id = 0 and a2.attr_type = 1")
                      ->select();
                  $new_attr_radio = array();
                  foreach ($attr_radio as $k => $v) {
                      $new_attr_radio[$v['attr_name']][] = $v;
                  }
                  $a=array();
                  $i=0;
                  foreach ($new_attr_radio as $k1=>$v1){

                      $a[$i]['attr_name']=$k1;
                      $j=0;
                      foreach ($v1 as $k2=>$v2){
                          $a[$i]['attr_value'][$j]['id']=$v2['id'];
                          $a[$i]['attr_value'][$j]['value']=$v2['attr_value'];
                          $j++;
                      }
                      $i++;
                  }
                  $data['attr'] = $a;

                  //单规格
                  $attr_only = M('Goods_attr')->alias('a1')
                      ->join('left join tpshop_attribute a2 on a1.attr_id = a2.attr_id')
                      ->where("a1.goods_id = {$data['id']} and a1.zy_goods_id = 0 and a2.attr_type = 0")
                      ->select();
                  $data['attr_only'] = $attr_only;

                  $pice = D('Goodspic')->field("picture_mid")->where("goods_id = {$data['id']} and status = 0 ")->select();
                  foreach($pice as $k=>&$v){
                      $v['picture_mid']=C('api_address').$v['picture_mid'];
                  }
                  $data['pic'] = $pice;
                  if(empty($data)){
                      json_Response(200, '传输成功', null, 1);
                  }else{
                      json_Response(200, '传输成功', $data, 1);
                  }

              } else {//自营
                  $data = D('Seller_selfgoods')->where("id = {$id}")->find();
                  $data['seller_id'] = $res['id'];
                  $data['seller_enterprise'] = $res['enterprise'];
                  $data['seller_phone'] = $res['phone'];
                  $data['seller_linkman'] = $res['linkman'];
                  if(empty($data)){
                      json_Response(200, '传输成功', [], 1);
                  }
                  $data['goods_introduce']=str_replace("/ueditor","http://www.jitailong.shop/ueditor",$data['goods_introduce']);
                  $pattern="/src=\"(.+?)\"/";
                  preg_match_all($pattern,$data['goods_introduce'],$match);
                  $data['goods_introduce']='';
                  foreach ($match[1] as $k=>$v){
                      $data['goods_introduce'][]['picture_mid']=$v;
                  }

                  //多规格
                  $attr_radio = M('Goods_attr')->alias('a1')
                      ->join('left join tpshop_attribute a2 on a1.attr_id = a2.attr_id')
                      ->where("a1.goods_id = {$data['id']} and a1.zy_goods_id = 0 and a2.attr_type = 1")
                      ->select();
                  $new_attr_radio = array();
                  foreach ($attr_radio as $k => $v) {
                      $new_attr_radio[$v['attr_name']][] = $v;
                  }
                  $a=array();
                  $i=0;
                  foreach ($new_attr_radio as $k1=>$v1){

                      $a[$i]['attr_name']=$k1;
                      $j=0;
                      foreach ($v1 as $k2=>$v2){
                          $a[$i]['attr_value'][$j]['id']=$v2['id'];
                          $a[$i]['attr_value'][$j]['value']=$v2['attr_value'];
                          $j++;
                      }
                      $i++;
                  }
                  $data['attr'] = $a;

                  //单规格
                  $attr_only = M('Goods_attr')->alias('a1')
                      ->join('left join tpshop_attribute a2 on a1.attr_id = a2.attr_id')
                      ->where("a1.goods_id = {$data['id']} and a1.zy_goods_id = 0 and a2.attr_type = 0")
                      ->select();
                  $data['attr_only'] = $attr_only;

                  $pice = D('Goodspic')->field("picture_mid")->where("goods_id = {$data['id']} and status = 0 ")->select();
                  foreach($pice as $k=>&$v){
                      $v['picture_mid']=C('api_address').$v['picture_mid'];
                  }
                  $data['pic'] = $pice;

                  json_Response(200, '传输成功', $data, 1);
              }

          }else{
              json_Response(-200,'传输失败',null,1);
          }
      }
      //加入购物车
      public function add_cart(){
          if(IS_POST) {
                  $model = D('Cart');
                  $data = I('post.');
                  if (empty($data['user_id'])||!is_numeric($data['user_id'])){
                      json_Response('-200','请先登录','null',2);
                  }
                  $count=D('User')->where("id={$data['user_id']}")->count();
                  if (!$count){
                      json_Response('-200','请先登录','null',2);
                  };
                  if (empty($data['goods_id'])||empty($data['goods_attr_ids'])||empty($data['seller_id'])||($data['supplier_id']=="")||empty($data['number'])){
                      json_Response(-200, '参数错误', null, 2);
                  }
                 if (!is_numeric($data['goods_id'])||!is_numeric($data['seller_id'])||!is_numeric($data['supplier_id'])||!is_numeric($data['number'])){
                     json_Response(-200, '参数错误', null, 2);
                 }
                 $count=D('Seller_user')->where("id={$data['seller_id']}")->count();
                 if (!$count){
                     json_Response('-200','参数错误','null',2);
                 };
                  $arr['user_id'] = $data['user_id'];
                  $arr['goods_id'] = $data['goods_id'];
                  $arr['goods_attr_ids'] = $data['goods_attr_ids'];
                  $arr['goods_attr_ids']=str_replace("，",",",$arr['goods_attr_ids']);
                  $arr['seller_id'] = $data['seller_id'];
                  $arr['supplier_id'] = $data['supplier_id'];
                  $cart = $model->where($arr)->find();
                  if (empty($cart)) {
                      $res =  $model->add($data);
                  }else{
                      $cart['number'] += $data['number'];
                      $res = $model->save($cart);
                  }
                  if ($res){
                      json_Response(200, '加入购物车成功', [], 2);
                  }else{
                      json_Response(200, '加入购物车失败', [], 2);
                  }

          }else{
              json_Response(-200,'传输失败',null,1);
          }
      }

}
